其他
ArkTS 入门指南:快速上手鸿蒙应用开发
The following article is from 陆业聪 Author 陆业聪
静态类型检查:ArkTS 在编译时进行类型检查,可以在代码运行前发现和修复错误。 更好的 IDE 支持:由于有了类型信息,IDE 可以提供更好的自动完成、导航和重构功能。 更易于维护和理解:类型注解和编译时检查使得代码更易于理解和维护。
打开 DevEco Studio,点击 File -> New -> Project,选择适合的模板作为项目类型。
输入项目名称、包名等信息,然后点击 Next。 点击 Finish,DevEco Studio 将自动创建一个 ArkTS 项目。
├── AppScope
│ ├── app.json5
│ └── resources
│ └── base
│ ├── element
│ │ └── string.json
│ └── media
│ └── app_icon.png
├── build-profile.json5
├── entry
│ ├── build-profile.json5
│ ├── hvigorfile.ts
│ ├── obfuscation-rules.txt
│ ├── oh-package.json5
│ └── src
│ ├── main
│ │ ├── ets
│ │ │ ├── entryability
│ │ │ │ └── EntryAbility.ets
│ │ │ └── pages
│ │ │ └── Index.ets
│ │ ├── module.json5
│ │ └── resources
│ │ ├── base
│ │ │ ├── element
│ │ │ │ ├── color.json
│ │ │ │ └── string.json
│ │ │ ├── media
│ │ │ │ ├── background.png
│ │ │ │ ├── foreground.png
│ │ │ │ ├── layered_image.json
│ │ │ │ └── startIcon.png
│ │ │ └── profile
│ │ │ └── main_pages.json
│ │ ├── en_US
│ │ │ └── element
│ │ │ └── string.json
│ │ ├── rawfile
│ │ └── zh_CN
│ │ └── element
│ │ └── string.json
│ ├── mock
│ │ └── mock-config.json5
│ ├── ohosTest
│ │ ├── ets
│ │ │ ├── test
│ │ │ │ ├── Ability.test.ets
│ │ │ │ └── List.test.ets
│ │ │ ├── testability
│ │ │ │ ├── TestAbility.ets
│ │ │ │ └── pages
│ │ │ │ └── Index.ets
│ │ │ └── testrunner
│ │ │ └── OpenHarmonyTestRunner.ets
│ │ ├── module.json5
│ │ └── resources
│ │ └── base
│ │ ├── element
│ │ │ ├── color.json
│ │ │ └── string.json
│ │ ├── media
│ │ │ └── icon.png
│ │ └── profile
│ │ └── test_pages.json
│ └── test
│ ├── List.test.ets
│ └── LocalUnit.test.ets
├── hvigor
│ └── hvigor-config.json5
├── hvigorfile.ts
├── local.properties
├── oh-package-lock.json5
├── oh-package.json5
└── oh_modules
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
RelativeContainer() {
Text(this.message)
.id('HelloWorld')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
}
.height('100%')
.width('100%')
}
}
鸿蒙工程的结构比较清晰,每个目录和文件都有明确的用途,这有助于开发者更好地理解和管理项目。
1. 类型注解
let message: string = "Hello, HarmonyOS";
let count: number = 10;
function greet(name: string): string {
return `Hello, ${name}`;
}
let greeting: string = greet("HarmonyOS");
2. 接口
interface Person {
name: string;
age: number;
}
function showPersonInfo(person: Person) {
console.log(`Name: ${person.name}, Age: ${person.age}`);
}
let person: Person = { name: "John", age: 30 };
showPersonInfo(person);
3. 类
class Animal {
private name: string;
constructor(name: string) {
this.name = name;
}
public speak(): void {
console.log(`${this.name} makes a noise.`);
}
}
let animal = new Animal("Dog");
animal.speak();
4. 继承
class Dog extends Animal {
speak(): void {
console.log(`${this.name} barks.`);
}
}
let dog = new Dog("Buddy");
dog.speak(); // Output: Buddy barks.
5. 泛型
function identity<T>(arg: T): T {
return arg;
}
let output1: string = identity<string>("myString");
let output2: number = identity<number>(10);
6. 模块
// math.ts
export function add(x: number, y: number): number {
return x + y;
}
export function subtract(x: number, y: number): number {
return x - y;
}
// app.ts
import { add, subtract } from './math';
console.log(add(10, 5)); // Output: 15
console.log(subtract(10, 5)); // Output: 5
7. 异步编程
async function fetchData(url: string): Promise<Data> {
let response = await fetch(url);
let data = await response.json();
return data;
}
fetchData('https://api.example.com/data')
.then(data => console.log(data))
.catch(error => console.error(error));
8. 类型别名
type Point = {
x: number;
y: number;
};
function drawPoint(point: Point): void {
console.log(`Drawing point at (${point.x}, ${point.y})`);
}
let point: Point = { x: 10, y: 20 };
drawPoint(point);
9. 类型保护
type Shape = Circle | Square;
function getArea(shape: Shape): number {
if (shape instanceof Circle) {
return Math.PI * shape.radius ** 2;
} else {
return shape.width * shape.height;
}
}
10. 枚举
enum Direction {
Up,
Down,
Left,
Right,
}
function move(direction: Direction): void {
console.log(`Moving in direction: ${Direction[direction]}`);
}
move(Direction.Up);
11. 映射类型
type ReadonlyPoint = Readonly<Point>;
let readonlyPoint: ReadonlyPoint = { x: 10, y: 20 };
readonlyPoint.x = 30; // Error: Cannot assign to 'x' because it is a read-only property
最后推荐一下我做的网站,玩Android: wanandroid.com ,包含详尽的知识体系、好用的工具,还有本公众号文章合集,欢迎体验和收藏!
推荐阅读:
扫一扫 关注我的公众号
如果你想要跟大家分享你的文章,欢迎投稿~
┏(^0^)┛明天见!